package montaltoController

import _root_.montaltoGrids.Grid
import scala.collection.mutable.Queue
import montaltoGrids._
import montaltoView._

/**
 * Created by IntelliJ IDEA.
 * User: Andrew Montalto
 * Date: May 1, 2010
 * Time: 8:55:01 PM
 * To change this template use File | Settings | File Templates.
 */

//client for the program runs all of the functionality
object Main extends Application{
   val landR = new LoadAndRunSim(40)
   landR.run()
   // val m = new Multiply(12,12)
   // m.show()
}





class LoadAndRunSim(totalNeds:Int){
//init grid
  var gridPopulator = null
  var gridSize = 8 //default size
  def run()={
    var grid:Grid = new Grid(gridSize)

    //refactored to a strategy pattern to allow for different
    //types of intial population
    val gridPopulater = new PopulateGridEvenXs(grid,totalNeds)
    gridPopulater.populate

    //register observers
    grid.RegisterObservers(new FindMaxNedsPerTurn)
    grid.RegisterObservers(new TotalTurns)
    grid.RegisterObservers(new OldestNed)
    grid.RegisterObservers(new MostMated)

    //create commands
    val growFoodCmd:GrowFoodCommand = new GrowFoodCommand(grid)
    val addFoodCmd:NedsAddFood = new NedsAddFood(grid)
    val nedsEatCmd:NedsEat = new NedsEat(grid)
    val cleanDeadOnes:CleanDeadNeds = new CleanDeadNeds(grid)
    val ageCmd:NedsAge = new NedsAge(grid)
    val turnCmd:TurnCommand = new TurnCommand(grid)
    val matecmd:MateNeds = new MateNeds(grid)
    val moveCmd:MoveCommand = new MoveCommand(grid)
    val moveToNextCmd:MoveToNedCommand = new MoveToNedCommand(grid)
    val turnOver:AddTurnToGrid = new AddTurnToGrid(grid)

  //add commands to controller
    val sc:SimController = new SimController
    sc.addCommands(growFoodCmd)
    sc.addCommands(addFoodCmd)
    sc.addCommands(nedsEatCmd)
    sc.addCommands(ageCmd)
    sc.addCommands(cleanDeadOnes)
    sc.addCommands(turnCmd)
    sc.addCommands(matecmd)
    sc.addCommands(moveCmd)
    sc.addCommands(moveToNextCmd)
    sc.addCommands(turnOver)

    //run simulator
    println("Starting sim\n population per 10 turns shown")
    var keepGoing:Boolean = true
    while(grid.countLivingNeds > 0 && keepGoing){
      sc.executeCommands  //run all the commands int the controller
      grid.Notify()  //Notify the observers

      //print out population over time
      if(grid.turn % 10 == 0){
      print(grid.countLivingNeds() + " ")
      }
    //stop an infinite loop
      if (grid.turn > 5000 ) {
       keepGoing = false
      }
    }

   println("\nending Sim")
    //show results
    println("results:")
    grid.PrintResults
  }
}

